package top.melody.ecommerce.platform.handler.handler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import top.melody.ecommerce.platform.handler.enums.AccountType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

public class AccountTypeHandler implements TypeHandler<AccountType> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, AccountType type, JdbcType jdbcType) throws SQLException {
        if(type == null){
            preparedStatement.setNull(i, Types.VARCHAR);
            return;
        }
        preparedStatement.setString(i, type.getCode());
    }

    @Override
    public AccountType getResult(ResultSet resultSet, String s) throws SQLException {
        String code = resultSet.getString(s);
        return AccountType.getByCode(code);

    }

    @Override
    public AccountType getResult(ResultSet resultSet, int i) throws SQLException {
        String code = resultSet.getString(i);
        return AccountType.getByCode(code);
    }

    @Override
    public AccountType getResult(CallableStatement callableStatement, int i) throws SQLException {
        String code = callableStatement.getString(i);
        return AccountType.getByCode(code);
    }
}
